package com.atguigu2.dao;

import com.beans.Customers;
import com.utils.JDBCUtilsJiuBan;
import org.junit.Test;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @auther user
 * @date 2021/12/10 - 2:00
 */
public class CustomersDaoImplTest {
    CustomersDaoImpl dao = new CustomersDaoImpl();

    @Test
    public void inert() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse("2000-01-02");
            Customers customers = new Customers(1, "于此奥菲", "yuciaofei@qq.com", new java.sql.Date(date.getTime()));
            dao.inert(connection,customers);
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void deleteById() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            dao.deleteById(connection,35);
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void updateById() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse("2000-01-02");
            Customers customers = new Customers(26, "于此奥菲", "yuciaofei@qq.com", new java.sql.Date(date.getTime()));
            dao.updateById(connection,customers);
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void getCustomersById() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            Customers customers = dao.getCustomersById(connection,19);
            System.out.println(customers);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void getAll() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            List<Customers> list = dao.getAll(connection);
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void getCount() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            Long count = dao.getCount(connection);
            System.out.println("count = " + count);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }

    @Test
    public void getMaxBirth() {
        Connection connection = null;
        try {
            connection = JDBCUtilsJiuBan.getConnection();
            java.sql.Date maxBirth = dao.getMaxBirth(connection);
            System.out.println("maxBirth = " + maxBirth);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtilsJiuBan.closeResource(connection,null);
        }
    }
}